/*
 * $Id: svn.c 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
 *
 * Copyright (c) 2006, 2007, 2008 Prabhu Gurumurthy <pgurumur@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * version "$Revision: 7557b5d4d82b $"
 * author "$Author: pgurumur $"
 */

#include "svn.h"

void
subversion_client(const char *program)
{
   if (svn_t) {
      if (svn_cmdline_init(program, stderr) != EXIT_SUCCESS)
         error("%s: svn_cmdline_init", __func__);

      svn_t->pool = svn_pool_create(NULL);
      svn_t->error = svn_fs_initialize(svn_t->pool);
      if (svn_t->error)
         error("%s: svn_fs_initialize", __func__);

      if ((svn_t->error = svn_client_create_context(&svn_t->ctx, svn_t->pool)))
         error("%s: svn_client_create_context", __func__);

      if ((svn_t->error = svn_config_get_config(
                  &svn_t->ctx->config, NULL, svn_t->pool)))
         error("%s: svn_config_get_config", __func__);

      svn_config_t *config = (svn_config_t *)apr_hash_get(svn_t->ctx->config,
            SVN_CONFIG_CATEGORY_CONFIG, APR_HASH_KEY_STRING);

      apr_array_header_t *providers = apr_array_make(svn_t->pool, 10,
            sizeof(svn_auth_provider_object_t *));
      svn_auth_get_platform_specific_client_providers(&providers,
            config, svn_t->pool);

      svn_auth_provider_object_t *provider;
      svn_auth_get_simple_prompt_provider(&provider, userpw_cb, svn_t, 2,
            svn_t->pool);
      APR_ARRAY_PUSH(providers, svn_auth_provider_object_t *) = provider;
      svn_auth_get_username_prompt_provider(&provider, user_cb, svn_t, 2,
            svn_t->pool);
      APR_ARRAY_PUSH(providers, svn_auth_provider_object_t *) = provider;

      svn_auth_get_ssl_server_trust_file_provider(&provider, svn_t->pool);
      APR_ARRAY_PUSH(providers, svn_auth_provider_object_t *) = provider;

      svn_auth_get_ssl_client_cert_file_provider(&provider, svn_t->pool);
      APR_ARRAY_PUSH(providers, svn_auth_provider_object_t *) = provider;

      svn_auth_get_ssl_client_cert_pw_file_provider2(&provider,
            plainpass_cb, svn_t, svn_t->pool);
      APR_ARRAY_PUSH(providers, svn_auth_provider_object_t *) = provider;

      svn_auth_get_ssl_server_trust_prompt_provider(&provider,
            sslserver_cb, svn_t, svn_t->pool);
      APR_ARRAY_PUSH(providers, svn_auth_provider_object_t *) = provider;

      svn_auth_get_ssl_client_cert_prompt_provider(&provider,
            sslclient_cb, svn_t, 2, svn_t->pool);
      APR_ARRAY_PUSH(providers, svn_auth_provider_object_t *) = provider;

      svn_auth_get_ssl_client_cert_pw_prompt_provider(&provider,
            sslpw_cb, svn_t, 2, svn_t->pool);
      APR_ARRAY_PUSH(providers, svn_auth_provider_object_t *) = provider;

      svn_auth_baton_t *auth_baton = NULL;
      svn_auth_open(&auth_baton, providers, svn_t->pool);

      svn_t->ctx->auth_baton = auth_baton;
      svn_t->client = 1;
   }
   else
      error("%s svn_t is NULL, tclsvn internal error", __func__);
}
